/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AuthPenSign.Database;

import AuthPenSign.AuthPenSign;
import AuthPenSign.Signature.Point;
import AuthPenSign.Signature.Signature;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author V-ray
 */
public class Database {

	String FileName;

	public Database() {
		this.FileName = "1.csv";
	}

	public Signature ReadSignature() {
		Double maxEigenValue = null;
		ArrayList<Point> tempArrayPoints = new ArrayList<>();
		try {
			try (BufferedReader br = new BufferedReader(new FileReader(this.FileName))) {
				maxEigenValue = Double.parseDouble(br.readLine());
				while (br.ready()) {
					String[] str = br.readLine().split(";");
					tempArrayPoints.add(new Point(Double.parseDouble(str[0]), Double.parseDouble(str[1]), Double.parseDouble(str[2]), Double.parseDouble(str[3])));
				}
			}
		} catch (FileNotFoundException ex) {
			Logger.getLogger(AuthPenSign.class.getName()).log(Level.SEVERE, null, ex);
		} catch (IOException ex) {
			Logger.getLogger(AuthPenSign.class.getName()).log(Level.SEVERE, null, ex);
		}
		return new Signature(tempArrayPoints.toArray(new Point[tempArrayPoints.size()]), maxEigenValue);
	}

	public void WriteSignature(Signature sign) {
		try {
			try (FileWriter file = new FileWriter(this.FileName)) {
				file.write(sign.getMaxEigenValue() + "\n");
				for (Point p : sign.getPoints()) {
					file.write(p.time + ";" + p.x + ";" + p.y + ";" + p.pressure + "\n");
				}
			}
		} catch (IOException ex) {
			Logger.getLogger(AuthPenSign.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}
